﻿using CallCenter.Common;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;

namespace CallCenter.Biz
{
    /// <summary>
    /// 电话呼入接口处理
    /// </summary>
    public class IncomingCall
    {
        /// <summary>
        /// 抢接
        /// </summary>
        /// <returns></returns>
        public bool ImcomingCallControl(string url, string enterpriceID)
        {
            // http://{{ServerIP}}:{{webAPIPort}}/SvrPBX.ashx?MethodName=CallControl
            string requestUrl = $"http://{url}/SvrPBX.ashx?MethodName=CallControl";
            Dictionary<string, string> pdata = new Dictionary<string, string>();

            // 接口版本号
            pdata.Add("version", "1.0");

            // 字符集
            pdata.Add("charset", "UTF_8");
            // 时间戳
            pdata.Add("timestamp", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

            pdata.Add("enterid", enterpriceID);
            // 请求人工号
            pdata.Add("appid", "700");
            // 操作坐席通道号
            pdata.Add("listenerfromch", "1");
            // 目标座席通道号
            pdata.Add("listenertoch", "1");
            pdata.Add("type", "4");

            pdata.Add("methodname", "CallControl");

            // 排序
            var s = pdata.OrderBy(e => e.Key);
            string str = "";
            foreach (var item in s)
            {
                str += item.Key + "=" + item.Value + "&";
            }

            str += "key=zfXAhqWhoBD";

            // 数据签名
            pdata.Add("sign", EncryptHelper.Md5Encrypt(str).ToUpper());
            // str += "&" + "sign=" + EncryptHelper.Md5Encrypt(str).ToUpper();

            pdata.Remove("methodname");            // 删除 
            string requestContent = JsonConvert.SerializeObject(pdata);
            string response = HttpHelper.HttpPost(requestUrl, requestContent);
            if (!string.IsNullOrEmpty(response))
            {
                HttpResponse responseMsg = JsonConvert.DeserializeObject<HttpResponse>(response);
                if (responseMsg != null)
                {
                    // 如果成功
                    if (responseMsg.RtnCode == 0)
                    {
                        if (responseMsg.data == null)
                        {
                            return false;
                        }

                        string content = responseMsg.data.ToString();
                        int index = content.IndexOf('[');
                        content = content.Remove(0, index + 1);

                        index = content.LastIndexOf(']');
                        content = content.Remove(index);
                        var temp = JsonConvert.DeserializeObject<Dictionary<string, object>>(content);
                        Dictionary<string, string> info = new Dictionary<string, string>();
                        foreach (var item in temp)
                        {
                            info.Add(item.Key, item.Value.ToString());
                        }

                        if (info.ContainsKey("code") && info["code"] == "200")
                        {
                            return true;
                        }
                    }
                }
            }

            return false;
        }
    }
}
